package edu.mse.camel.client.playback.actions;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPart;

import edu.mse.camel.client.playback.Activator;
import edu.mse.camel.client.playback.events.ReceiveGenericPeerEventList;
import edu.mse.camel.client.session.team.TEAMMEMBER_STATUS;
import edu.mse.camel.client.session.team.TeamList;

public class NextActionDelegate implements IObjectActionDelegate,
IViewActionDelegate {

	private int index=0;
	private int size=0;
	
	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void run(IAction action) {
		
		if(!TeamList.getInstance().getMyStatus().equals(TEAMMEMBER_STATUS.SESSION) ){
			ErrorDialog ed = new ErrorDialog(new Shell(),
					"Error",
					"You are not in playback session. Unable to move forward.", 
					new Status(
							IStatus.ERROR, Activator.PLUGIN_ID,
							"You are not in playback session. Unable to move forward."),
					IStatus.ERROR);
			ed.open();							
		} 
		else {
			size = ReceiveGenericPeerEventList.getInstance().getEventList().size();
			index = ReceiveGenericPeerEventList.getInstance().getCurrentIndex();
			index ++;
			if(index >= size)
			{
				ErrorDialog ed = new ErrorDialog(new Shell(),
						"Error",
						"You already reached the end. Unable to move forward.", 
						new Status(
								IStatus.ERROR, Activator.PLUGIN_ID,
								"You already reached the end. Unable to move forward."),
						IStatus.ERROR);
				ed.open();				
			} else
				ReceiveGenericPeerEventList.getInstance().nextEvent(index);
		}
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(IViewPart view) {
		// TODO Auto-generated method stub
		
	}

}
